package com.geostar.constructionland.attachment.service.impl;

import com.geostar.constructionland.attachment.entity.FileDownVo;
import com.geostar.constructionland.attachment.entity.GeoAttachment;
import com.geostar.constructionland.attachment.mapper.GeoAttachmentMapper;
import com.geostar.constructionland.attachment.service.IAttachmentService;
import com.geostar.constructionland.attachment.service.IPreviewService;
import com.geostar.constructionland.attachment.service.IStorageService;
import com.geostar.constructionland.attachment.service.StorageServiceFactory;
import com.geostar.constructionland.attachment.util.LocalTmpPathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Description 预览服务实现
 * @Date 2021/1/21 11:14
 * @Created by 37315
 * @author yell
 */
@Service
public class PreviewServiceImpl implements IPreviewService {
    StorageServiceFactory storageServiceFactory;
    @Autowired
    public void setStorageServiceFactory(StorageServiceFactory storageServiceFactory) {
        this.storageServiceFactory = storageServiceFactory;
    }
    GeoAttachmentMapper geoAttachmentMapper;
    @Autowired
    public void setGeoAttachmentMapper(GeoAttachmentMapper geoAttachmentMapper) {
        this.geoAttachmentMapper = geoAttachmentMapper;
    }
    LocalTmpPathUtil localTmpPathUtil;
    @Autowired
    public void setLocalTmpPathUtil(LocalTmpPathUtil localTmpPathUtil) {
        this.localTmpPathUtil = localTmpPathUtil;
    }

    @Autowired
    IAttachmentService attachmentService;

    @Override
    public FileDownVo getPreFile(String id) {
        IStorageService storageService = storageServiceFactory.getCurrentService();
        // 查找文件
        GeoAttachment geoAttachment = geoAttachmentMapper.selectById(id);
        if(geoAttachment != null){
            // 下载文件
            byte[] bytes = storageService.getByteArrayByFilePath(geoAttachment.getFilePath());
            FileDownVo fileDownVo = new FileDownVo();
            fileDownVo.setBytes(bytes);
            fileDownVo.setFileName(geoAttachment.getName());
            fileDownVo.setFileSize(geoAttachment.getFileSize());
            return fileDownVo;
        }
        return null;
    }
}
